package com.robot.base.view.binding;

import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.robot.base.R;
import com.robot.base.common.api.GlBaseApi;
import com.robot.base.util.AppUtils;
import com.robot.base.utils.pic.ImageLoaderUtils;

import androidx.databinding.BindingAdapter;

/**
 * 创建日期：2020/10/22  14:41
 * 类说明:
 *
 * @author：robot
 */
public class ImgaeViewBinding {
    @BindingAdapter("android:loadRound4")
    public static void loadRound4(ImageView imageView, String avatar) {
        if (avatar.contains("http")) {
            ImageLoaderUtils.displayRound(AppUtils.getApp(), imageView, avatar, 4);
        } else {
            ImageLoaderUtils.displayRound(AppUtils.getApp(), imageView, GlBaseApi.PIC_BASE_URL + avatar, 4);
        }
    }

    @BindingAdapter("android:loadAvatar")
    public static void loadAvatar(ImageView imageView, String avatar) {
        if (TextUtils.isEmpty(avatar)) {
            avatar = "https://ep.watsons.com.cn/MPOSImage/3456_sco_home_pic.png";
        }
        if (avatar.contains("http")) {
            ImageLoaderUtils.displayCircle(AppUtils.getApp(), imageView, avatar);
        } else {
            ImageLoaderUtils.displayCircle(AppUtils.getApp(), imageView, GlBaseApi.PIC_BASE_URL + avatar);
        }
    }

    @BindingAdapter("android:loadPic")
    public static void loadPic(ImageView imageView, String avatar) {
        ImageLoaderUtils.display(AppUtils.getApp(), imageView, avatar);
    }

    @BindingAdapter("android:loadLocalPic")
    public static void loadLocalPic(ImageView imageView, String avatar) {
        RequestOptions options = new RequestOptions().centerCrop().placeholder(R.drawable.ic_loading_placeholder).error(R.mipmap.ic_gallery_img_empty).priority(Priority.HIGH).diskCacheStrategy(DiskCacheStrategy.ALL);
        ImageLoaderUtils.getGlideManager().load(avatar).apply(options).into(imageView);
    }


    @BindingAdapter(value = {"android:loadLocalPicAndDefault"}, requireAll = false)
    public static void loadLocalPicAndDefault(ImageView imageView, String avatar) {
        RequestOptions options = new RequestOptions().fitCenter().placeholder(R.drawable.ic_default_avatar).error(R.drawable.ic_default_avatar).priority(Priority.HIGH).diskCacheStrategy(DiskCacheStrategy.ALL);
        ImageLoaderUtils.getGlideManager().load(avatar).apply(options).into(imageView);
    }

    @BindingAdapter("android:loadResPic")
    public static void loadResPic(ImageView imageView, int avatar) {
        RequestOptions options = new RequestOptions().centerCrop().placeholder(R.drawable.ic_loading_placeholder).error(R.mipmap.ic_gallery_img_empty).priority(Priority.HIGH).diskCacheStrategy(DiskCacheStrategy.ALL);
        ImageLoaderUtils.getGlideManager().load(avatar).apply(options).into(imageView);
    }
}
